<?php
class Administrator_Model_User extends Administrator_Model_System {
	public function __construct() {
		$this->lang = new Nguyen_language_system ();
		$this->db = Zend_Registry::get ( 'vicdbsystem' );
	}
	
	public function GetUserInfo($id) {
		$sql = 'select id,username,image,name,email,gender,birthday,address,type,status,phone from user where id="' . $id . '"';
		return $this->db->fetchRow ( $sql );
	}
	public function GetCheckUsername($name){
		$sql = 'select * from user where username="'.$name.'"';
		$result =  $this->db->fetchAll ( $sql );
		return count($result);
	}
	public function InsertUser($username, $pass, $image, $name, $email, $type, $status, $phone) {
		$data = array ('username' => $username, 'password' => $pass, 'image' => $image, 'name' => $name, 'email' => $email, 'type' => $type, 'status' => $status, 'phone' => $phone );
		$this->db->insert ( 'user', $data );
		return $this->db->lastInsertId ();
	}
	public function GetAllUser() {
		$sql = 'select * from user';
		return $this->db->fetchAll ( $sql );
	}
	public function UpdateUser($id,$utype, $username, $pass, $image, $name, $email, $type, $phone) {
		$info = $this->GetUserInfo($id);
		if($info['type']<$utype)return ;
		$where = "id = '$id'";
		$data = array ('username' => $username, 'password' => $pass, 'image' => $image, 'name' => $name, 'email' => $email, 'type' => $type, 'phone' => $phone );
		$this->db->update ( 'user', $data, $where );
	}
	public function UpdateUsernoPass($id,$utype, $username, $image, $name, $email, $type,  $phone) {
		$info = $this->GetUserInfo($id);
		if($info['type']<$utype)return ;
		$where = "id = '$id'";
		$data = array ('username' => $username, 'image' => $image, 'name' => $name, 'email' => $email, 'type' => $type, 'phone' => $phone );
		$this->db->update ( 'user', $data, $where );
	}
	public function DelUser($id,$type) {
		$info = $this->GetUserInfo($id);
		try {
			$this->DeleteImageOnHost('user', $info['image']);
		} catch (Exception $e) {
		}
		if($info['type']<$type)return ;
		$where = "id='" . $id . "'";
		$this->db->delete ( 'user', $where );
	}
	public function GetAllUser_Type(){
		$sql = 'select * from user_type';
		return $this->db->fetchAll ( $sql );
	}
	public function GetUser_Type($id){
		$sql = 'select name from user_type where id='.$id;
		$result =  $this->db->fetchRow ( $sql );
		return $result['name'];
	}
	public function GetCheckValidPass($id,$pass){
			
		$sql = 'select * from user where id="'.$id.'" and password="'.md5($pass).'"';
		$result =  $this->db->fetchRow ( $sql );
		return count($result);
	}
	public function Updatepass($id,$pass)
	{
		$where = "id = '$id'";
		$data = array (
			'password' => $pass	,
		);
		$this->db->update ( 'user', $data, $where );
	}
	public function InsertUserAll($username, $pass, $image, $name,$birthday,$gender,$address, $email, $type, $status, $phone){
		$data = array ('username' => $username, 
						'password' => $pass, 
						'image' => $image, 
						'name' => $name,
						'birthday' =>$birthday,
						'gender' =>$gender,
						'address' =>$address,
						 'email' => $email, 
						 'type' => $type, 
						 'status' => $status, 
						 'phone' => $phone );
		$this->db->insert ( 'user', $data );
		return $this->db->lastInsertId ();
	}


	public function UpdateUserAll($id,$utype, $username, $pass, $image, $name, $email, $type, $phone,$birthday,$gender,$address) {
		$info = $this->GetUserInfo($id);
		if($info['type']<$utype)return ;
		$where = "id = '$id'";
		$data = array (
			'username' => $username, 
			'password' => $pass, 
			'image' => $image, 
			'name' => $name, 
			'email' => $email, 
			'type' => $type,
			 'phone' => $phone, 
			'birthday' =>$birthday,
			'gender' =>$gender,
			'address' =>$address);
		$this->db->update ( 'user', $data, $where );
	}



	public function UpdateUserNoPassAll($id,$utype, $username, $image, $name, $email, $type, $phone,$birthday,$gender,$address) {
		$info = $this->GetUserInfo($id);
		if($info['type']<$utype)return ;
		$where = "id = '$id'";
		$data = array (
			'username' => $username, 
			'image' => $image, 
			'name' => $name, 
			'email' => $email, 
			'type' => $type,
			 'phone' => $phone, 
			'birthday' =>$birthday,
			'gender' =>$gender,
			'address' =>$address);
		$this->db->update ( 'user', $data, $where );
	}
}